﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

public partial class Order_PostData : System.Web.UI.Page
{

    public struct OrderedItem
    {
        public String itemName;
        public int item_id;
        public double unitPrice;
        public int quantity;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
        System.Text.StringBuilder displayValues =
       new System.Text.StringBuilder();
        System.Collections.Specialized.NameValueCollection
            postedValues = Request.Form;
        String nextKey;
        String strValue;

        int iItemID = -1;
        int iItemQty = -1;
        int iItemPrice = -1;
        double dTotal = 0;
        Hashtable htItems = new Hashtable();
        for (int i = 0; i < postedValues.AllKeys.Length; i++)
        {
            OrderedItem ordIt = new OrderedItem();
            nextKey = postedValues.AllKeys[i];
            if (nextKey.Substring(0, 2) != "__")
            {
                displayValues.Append("<br>");
                displayValues.Append(nextKey);
                displayValues.Append(" = ");
                displayValues.Append(postedValues[i]);
                
                strValue = postedValues[i].ToString();

                try
                {
                    if (nextKey.IndexOf("_") > 0)
                    {
                        iItemID = int.Parse(nextKey.Substring(nextKey.IndexOf("_") + 1).Trim());
                    }
                    else
                    {
                        iItemID = 1;
                    }
                    iItemPrice = int.Parse(strValue.Substring(strValue.IndexOf(",") + 1).Trim());
                    iItemQty = int.Parse(postedValues[i].Substring(0, strValue.IndexOf(",") ).Trim());

                    ordIt.item_id = iItemID;
                    ordIt.quantity = iItemQty;
                    ordIt.unitPrice = iItemPrice;
                    htItems.Add(iItemID, ordIt);
                    dTotal += (iItemQty * iItemPrice);
                    
                }
                catch (Exception)
                {
                    
                }
            }
        }

        displayValues.Append("<br> TOTAL = " + dTotal);
        Label1.Text += postedValues.Count + "[]";
        Label1.Text += displayValues.ToString();


        DateTime orderRecvTime = System.DateTime.Now;
        String strOrderID = "" + orderRecvTime.Year + orderRecvTime.Month + orderRecvTime.Day
            + orderRecvTime.Hour + orderRecvTime.Minute + orderRecvTime.Second;            

        OleDbConnection objOLEDB = new OleDbConnection();
        OleDbCommand objOLECmd = new OleDbCommand();
        string _sqlMenu = "";
        //_sqlMenu = "INSERT INTO `order` (`client_order_id`, `order_date`, `order_type`, `order_amount" +
          //              "`) VALUES (@client_order_id, @order_date, @order_type, @order_amount )";

        
            objOLEDB.ConnectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
            objOLEDB.Open();
            objOLECmd.Connection = objOLEDB;

            _sqlMenu = "INSERT INTO [order] ([client_order_id],[order_date],[order_type],[order_amount]) " +
                                             " Values('" + strOrderID + "','" + DateTime.Now + "','" +
                                             "Dine In" + "'," + dTotal + ")";

 
            objOLECmd.CommandText = _sqlMenu;
            
            
            objOLECmd.ExecuteNonQuery();


            foreach (OrderedItem iID in htItems.Values)
            {
                _sqlMenu = "INSERT INTO [order_desc] ([order_id],[item_id],[quantity],[unit_price]) " +
                                                    " Values('" + strOrderID + "'," + iID.item_id + "," +
                                                    iID.quantity + "," + iID.unitPrice + ")";

                objOLECmd.CommandText = _sqlMenu;


                objOLECmd.ExecuteNonQuery();
            }


            objOLEDB.Close();

        }
        catch (Exception ex)
        {
            Label1.Text += ex.Message;
        }

       

    }
}